package com.zzzzzz.account;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.zzzzzz.common.page.Page;
import com.zzzzzz.core.dao.jdbc.BaseDao;

@Repository
public class UserDao extends BaseDao {
	
	private final static String ADD = 				"insert into user(email, password, name) values(:email, :password, :name)";
	private final static String EDIT_STATUS = 		"update user set status = ?, upd_time = ? where id = ?";
	private static final String EDIT_BASE = 		"update user set name = :name, gender = :gender, about_me = :aboutMe, upd_time = :updTime where id = :id";
	private static final String EDIT_PIC = 			"update user set pic_id = ? where id = ?";
	private static final String EDIT_PASSWORD = 	"update user set password = :newPassword where id = :userId and password = :currentPassword";
	private static final String EDIT_EMAIL = 		"update user set email = :email where id = :userId and password = :password";
	
	private final static String FIND_BY_ID = 		"select id,email,password,name,gender,about_me,pic_id,add_time,upd_time from user where id = ? and status = 0";
	private final static String FIND_BY_EMAIL = 	"select id,email,password,name,gender,about_me,pic_id,add_time,upd_time from user where email = ? and status = 0";
	
	private final static String FIND_ALL = 			"select id,email,name,gender,about_me,pic_id,add_time,upd_time from user where status = 0";
	private final static String FIND_ALL_COUNT = 	"select count(1) as count from user where status = 0";

	protected class UserRowMapper implements RowMapper<User> {
		public User mapRow(ResultSet rs, int rn) throws SQLException {
			User user = new User();
			user.setId(rs.getLong("id"));
			user.setPassword(rs.getString("password"));
			user.setEmail(rs.getString("email"));
			user.setName(rs.getString("name"));
			user.setGender(rs.getString("gender"));
			user.setAboutMe(rs.getString("about_me"));
			user.setPicId(rs.getString("pic_id"));
			user.setAddTime(rs.getTimestamp("add_time"));
			user.setUpdTime(rs.getTimestamp("upd_time"));
			return user;
		}
	}
	
	protected class UserViewRowMapper implements RowMapper<UserView> {
		public UserView mapRow(ResultSet rs, int rn) throws SQLException {
			UserView userView = new UserView();
			userView.setUserId(rs.getLong("id"));
			userView.setEmail(rs.getString("email"));
			userView.setName(rs.getString("name"));
			userView.setGender(rs.getString("gender"));
			userView.setAboutMe(rs.getString("about_me"));
			userView.setPicId(rs.getString("pic_id"));
			userView.setAddTime(rs.getTimestamp("add_time"));
			userView.setUpdTime(rs.getTimestamp("upd_time"));
			return userView;
		}
	}
	
	public void add(User user) {
		Long id = updateForLongKey(ADD, user);
		user.setId(id);
	}

	public void edit(UserEditForm userEditForm) {
		update(EDIT_BASE, userEditForm);
	}
	
	public void editStatus(Long id, String status, Date updTime) {
		update(EDIT_STATUS, status, updTime, id);
	}
	
	public void editPic(Long id, String picId) {
		update(EDIT_PIC, picId, id);
	}
	
	public void editEmail(UserEmailEditForm userEmailEditForm) {
		update(EDIT_EMAIL, userEmailEditForm);
	}
	
	public void editPassword(UserPasswordEditForm userPasswordEditForm) {
		update(EDIT_PASSWORD, userPasswordEditForm);
	}
	
	public User findById(Long id) {
		return findOne(FIND_BY_ID, new UserRowMapper(), id);
	}

	public User findByEmail(String email) {
		return findOne(FIND_BY_EMAIL, new UserRowMapper(),email);
	}
	
	public Page<UserView> findAll(Page<UserView> page) {
		MapSqlParameterSource paramMap = new MapSqlParameterSource();
		StringBuilder sql = new StringBuilder(FIND_ALL);
		sql.append(limit(paramMap,page));
		return findPage(page, sql.toString(), FIND_ALL_COUNT, paramMap , new UserViewRowMapper());
	}
}
